package edu.up.taglibs;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import edu.up.globalizacao.LabelReader;

public class ResourceBundleTag extends TagSupport {
	private static final long serialVersionUID = -6851091722941797932L;
	private String property = null;
	private LabelReader label;
	private boolean bold;

	public final static String paramCountry = "country";

	/**
	 * Constructor for ErrorTag.
	 */
	public ResourceBundleTag() {
		super();
	}

	public int doStartTag() throws JspException {

		String countryBarraEndereco = (String) pageContext.getRequest().getParameter(
				paramCountry);

		String countrySession = (String) pageContext.getSession().getAttribute(
				paramCountry);

		if (countrySession == null || countrySession.length() == 0) {
			countryBarraEndereco = "pt_BR";
			pageContext.getSession().setAttribute(paramCountry, countryBarraEndereco);
			pageContext.getRequest().setAttribute(paramCountry, countryBarraEndereco);
			// pageContext.setAttribute(paramCountry, country);
		} else {
			if (countryBarraEndereco == null)
				countryBarraEndereco = countrySession;
			pageContext.getSession().setAttribute(paramCountry, countryBarraEndereco);
			pageContext.getRequest().setAttribute(paramCountry, countryBarraEndereco);
			// pageContext.setAttribute(paramCountry, country);

	
		}
		this.label = new LabelReader(countryBarraEndereco);
		return super.doStartTag();
	}

	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			if (this.isBold()) {
				out.print("<B>");
			}
			out.print(this.label.getProperty(property));
			if (this.isBold()) {
				out.print("</B>");
			}
		} catch (Exception e) {
			throw new JspException(e);
		}
		return EVAL_PAGE;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public boolean isBold() {
		return bold;
	}

	public void setBold(boolean bold) {
		this.bold = bold;
	}
}